﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using System;


namespace ConfigurationFileDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            #region MyRegion
            //var builder = new ConfigurationBuilder();

            ////需要打开文件的监视
            //builder.AddJsonFile("appsettings.json",optional:true,reloadOnChange:true);//,optional:  false,reloadOnChange:false
            ////builder.AddIniFile("appsettings.ini");
            ////builder.AddJsonFile("appsetting.Development.json");
            //var configurationRoot = builder.Build();


            ////这个方法是没有入参的。他直接返回了一个IChangeToken的类型 ，这个类型在 Microsoft.Extensions.Primitives命名空间下
            //IChangeToken token = configurationRoot.GetReloadToken();
            ////仅仅在配置发生变化时才进行输出
            //token.RegisterChangeCallback(state => 
            //{
            //    Console.WriteLine($"key1:{configurationRoot["key1"]}");
            //    Console.WriteLine($"key2:{configurationRoot["key2"]}");
            //    Console.WriteLine($"key3:{configurationRoot["key3"]}");
            //    Console.WriteLine($"key4:{configurationRoot["key4"]}");
            //}, configurationRoot);

            //Console.ReadKey();

            //ChangeToken.OnChange(() => configurationRoot.GetReloadToken(), () =>
            //{
            //    Console.WriteLine($"key1:{configurationRoot["key1"]}");
            //    Console.WriteLine($"key2:{configurationRoot["key2"]}");
            //    Console.WriteLine($"key3:{configurationRoot["key3"]}");
            //    Console.WriteLine($"key4:{configurationRoot["key4"]}");
            //});
            //Console.WriteLine("开始了。。。");
            //Console.ReadKey();




            //Console.WriteLine($"key1:{configurationRoot["key1"]}");
            //Console.WriteLine($"key2:{configurationRoot["key2"]}");
            //Console.WriteLine($"key3:{configurationRoot["key3"]}");
            //Console.WriteLine($"key4:{configurationRoot["key4"]}");
            //Console.ReadKey();  
            #endregion


            #region 强类型绑定配置
            var builder = new ConfigurationBuilder();
            builder.AddJsonFile("appsettings.json");
            var configurationRoot = builder.Build();

            var config = new Config()
            {
                Key1 = "config key1",
                Key5 = false,
                //Key6 = 100
            };

            //configurationRoot.Bind(config);
            configurationRoot.GetSection("OrderService").Bind(config, configureOptions =>
            {
                configureOptions.BindNonPublicProperties = true;
            });
            Console.WriteLine($"Key1:{config.Key1}");
            Console.WriteLine($"Key5:{config.Key5}");
            Console.WriteLine($"Key6:{config.Key6}");

            #endregion
        }
    }

    class Config
    {
        public string Key1 { get; set; }
        public bool Key5 { get; set; }
        public int Key6 { get; private set; } = 100;
    }
}
